package com.booter.consumer.controller;

import com.booter.consumer.config.ConstantsConfig;
import com.booter.consumer.service.MerchantClientService;
import com.booter.consumer.service.ProducerClientService;
import com.booter.model.MerchantOrder;
import org.apache.shardingsphere.core.strategy.keygen.SnowflakeShardingKeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;

//import com.alibaba.csp.sentinel.annotation.SentinelResource;
//import com.booter.consumer.handler.SentinelExceptionHandler;

/**
 * @author admin
 */
@RequestMapping("user")
@RestController
public class ConsumerController {

    private static SnowflakeShardingKeyGenerator keyGenerator = new SnowflakeShardingKeyGenerator();


    @Autowired
    private ProducerClientService producerClientService;

    @Autowired
    private MerchantClientService merchantClientService;


    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ConstantsConfig config;


    /**
     * 节点和服务注销
     * 节点和服务的注销可以使用HTTP API:
     * 注销任意节点和服务：/catalog/deregister
     * 注销当前节点的服务：/agent/service/deregister/:service_id
     */
//    @Autowired
//    private ConsulClient consulClient;

    /**
     * 用于监控检查 ，若集成了actuator，则默认的健康检查地址是actuator/health
     */

    @RequestMapping(value = "list")
    public String index(@RequestParam String id) {
        System.out.println("time=" + System.currentTimeMillis());
        return this.producerClientService.index(id);
    }

    @RequestMapping("update")
    public String update(@RequestBody Map<String, Object> param) {
        return "{}";
    }

    @RequestMapping(value = "add")
    public String add(@RequestParam String id) {
        System.out.println("time=" + System.currentTimeMillis());
        return this.producerClientService.index(id);
    }

    @RequestMapping(value = "get")
    public String get(@RequestParam String id, HttpServletRequest request) {
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = (String) headerNames.nextElement();
            System.out.println(headerName + " : " + request.getHeader(headerName));
        }
        System.out.println("time=" + System.currentTimeMillis());
        return this.producerClientService.index(id);
    }

    @RequestMapping(value = "post")
    public Map<String, Object> post(@RequestBody Map<String, Object> params) {
        System.out.println("size=" + params.size());
        return params;
    }

//    @RequestMapping(value = "/unregister")
//    public String unregister(@RequestParam String id) {
//        this.consulClient.agentServiceDeregister(id);
//        return "ok";
//    }


    @RequestMapping(value = "/get2")
    public String get2(@RequestParam String id) {
        return this.restTemplate.getForObject("http://booter-producer/user/index2?id=" + id, String.class);
    }


    @RequestMapping(value = "/properties")
    public String getProperties() {
        return this.config.getRedis() + "," + this.config.getTitle();
    }

    @RequestMapping(value = "merchant/order/create")
    public Object createMerchantOrder() {
        MerchantOrder mo = new MerchantOrder();
        mo.setMid((Long) keyGenerator.generateKey());
        mo.setOrderId((Long) keyGenerator.generateKey());
        return this.merchantClientService.create(mo);
    }

}
